package ligne2vie.topos.client.maps.proxy;

import java.io.Serializable;

import com.google.gwt.maps.client.HasMap;
import com.google.gwt.maps.client.overlay.HasMarkerImage;
import com.google.gwt.maps.client.overlay.HasMarkerOptions;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.maps.client.overlay.MarkerOptions;
import com.google.gwt.maps.client.overlay.impl.MarkerImpl;

/**
 * The class {@link MyMarker} extends {@link Marker} in order to correct and add functionalities.
 * 
 * @author thuguerre
 * @since 1.0.0
 */
public class MyMarker extends Marker implements Serializable {

	/**
	 * Default Serial UID.
	 */
	private static final long serialVersionUID = -3273117961458098836L;

	/**
	 * Default constructor.
	 * 
	 * @since 1.1.0
	 */
	public MyMarker() {
		super();
	}

	/**
	 * Constructor with options.
	 * 
	 * @param options
	 *            Options to give to marker.
	 * @since 1.1.0
	 */
	public MyMarker(HasMarkerOptions options) {
		super(options);
	}

	/**
	 * The method {@link #build(String)} returns a {@link MyMarker} having as icon the image whose URL is given as
	 * parameter. The returned marker is not linked to any map and does not have a set position. Up to the using method
	 * to set these two values.
	 * 
	 * @param imageUrl
	 *            Url of the image to use for the marker.
	 * @return A {@link MyMarker} with a specific icon.
	 * @since 1.1.0
	 */
	public static MyMarker build(String imageUrl) {

		HasMarkerImage image = MyMarkerImage.build(imageUrl);

		HasMarkerOptions options = new MarkerOptions();
		options.setVisible(true);
		options.setIcon(image);
		options.setClickable(true);

		return new MyMarker(options);
	}

	@Override
	public void setMap(HasMap map) {
		if (map == null) {
			MarkerImpl.impl.setMap(getJso(), null);
		} else {
			super.setMap(map);
		}
	}
}
